<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入姓名" ></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24" >
            <a-form-model-item label="户籍所在地" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jiguan">
              <a-input v-model="model.jiguan" placeholder="请输入户籍所在地" ></a-input>
            </a-form-model-item>
          </a-col> -->
<!--          <a-col :span="24" >
            <a-form-model-item label="出生年月" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="birth">
              <j-date placeholder="请选择出生年月" v-model="model.birth" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sex">
              <j-dict-select-tag type="list" v-model="model.sex" dictCode="sex" placeholder="请选择性别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="年龄" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="age">
              <a-input-number v-model="model.age" placeholder="请输入年龄" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24" >
            <a-form-model-item label="证件类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zjlx">
              <j-dict-select-tag type="list" v-model="model.zjlx" dictCode="card_type" placeholder="请选择证件类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sfzh">
              <a-input v-model="model.sfzh" placeholder="请输入身份证号" ></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24" >
            <a-form-model-item label="政治面貌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zzmm">
              <j-dict-select-tag type="list" v-model="model.zzmm" dictCode="sys_zzmm" placeholder="请选择政治面貌" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="退役军人类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tyjrlx">
              <j-dict-select-tag type="list" v-model="model.tyjrlx" dictCode="sys_tyjrlx" placeholder="请选择退役军人类型" />
            </a-form-model-item>
          </a-col> -->
<!--          <a-col :span="24" >
            <a-form-model-item label="优抚类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="yflx">
              <j-dict-select-tag type="list" v-model="model.yflx" dictCode="sys_yflx" placeholder="请选择优抚类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="入伍时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rwsj">
              <j-date placeholder="请选择入伍时间" v-model="model.rwsj" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="退伍时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="twsj">
              <j-date placeholder="请选择退伍时间" v-model="model.twsj" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24" >
            <a-form-model-item label="优待证号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ydzh">
              <a-input v-model="model.ydzh" placeholder="请输入优待证号" ></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24" >
            <a-form-model-item label="家庭住址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jtzz">
              <a-input v-model="model.jtzz" placeholder="请输入家庭住址" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="安置地" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="azd">
              <a-input v-model="model.azd" placeholder="请输入安置地" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="头像" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="avtar">
              <j-image-upload isMultiple  v-model="model.avtar" ></j-image-upload>
            </a-form-model-item>
          </a-col> -->
<!--          <a-col :span="24" >
            <a-form-model-item label="用户ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userid">
              <j-select-user-by-dep v-model="model.userid" />
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="电子优待证信息表" :key="refKeys[0]" :forceRender="true">
        <sys-veterans-card-form ref="sysVeteransCardForm" @validateError="validateError" :disabled="formDisabled"></sys-veterans-card-form>
      </a-tab-pane>
      
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import SysVeteransCardForm from './SysVeteransCardForm.vue'
  import { duplicateCheck } from '@/api/api'
  export default {
    name: 'SysVeteransForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
      SysVeteransCardForm,
    },
    data() {
      return {
         userId:"", //保存用户id
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
         },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           lxfs: [{ required: true},{ message: '请输入正确的手机号码!'}, {validator: this.validatelxfs}],
           name:[{ required: true, message: '请输入用户姓名!' }],
           sfzh:[{ required: true, message: '请输入用户身份证号码!' }, {validator: this.validatesfzh}],
           ydzh:[{ required: true, message: '请输入用户优待证号码!' }, {validator: this.validatesfzh}],
        },
        refKeys: ['sysVeteransCard', ],
        tableKeys:[],
        activeKey: 'sysVeteransCard',
        // 电子优待证信息表
        sysVeteransCardTable: {
          loading: false,
          dataSource: [],
          columns: [
          ]
        },
        url: {
          add: "/veterans/sysVeterans/add",
          edit: "/veterans/sysVeterans/edit",
          queryById: "/veterans/sysVeterans/queryById",
          sysVeteransCard: {
            list: '/veterans/sysVeterans/querySysVeteransCardByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.$refs.sysVeteransCardForm.clearFormData()
      },
      getAllTable() {
        return new Promise(resolve => {
          resolve([]);
        })
      },
       //手机号码校验及查重
      validatelxfs(rule, value, callback){
        if(!value){
          callback()
        }else{
          if(new RegExp(/^1[3|4|5|7|8|9][0-9]\d{8}$/).test(value)){
            var params = {
              tableName: 'sys_veterans',
              fieldName: 'lxfs',
              fieldVal: value,
              dataId: this.userId
            };
            duplicateCheck(params).then((res) => {
              if (res.success) {
                callback()
              } else {
                callback("手机号已存在!")
              }
            })
          }else{
            callback("请输入正确格式的手机号码!");
          }
        }
      },
      //身份证号码校验及查重
      validatesfzh(rule, value, callback){
        var params = {
          tableName: 'sys_veterans',
          fieldName: 'sfzh',
          fieldVal: value,
          dataId: this.userId
        };
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback("身份证号码已存在!")
          }
        })
      },
      
      
      
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
          this.$refs.sysVeteransCardForm.initFormData(this.url.sysVeteransCard.list,this.model.id)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
                  this.$refs.sysVeteransCardForm.validate(0),
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          sysVeteransCardList: this.$refs.sysVeteransCardForm.getFormData(),
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>